package com.powerManager.sdk.web;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;

import com.powerManager.sdk.common.ISecurityUser;
import com.powerManager.sdk.common.Security;
import com.powerManager.sdk.utils.SysUtils;


/** 请求数据处理
 * @author Administrator
 *
 */
public class RequestProcess {
	
	private Logger LOG = Logger.getLogger(RequestProcess.class);
	
	private static final String DEFAULT_METHOD="execute";

	/** 功能请求分配
	 * @param request
	 * @param response
	 * @param path
	 * @param arguments
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String doProcess(HttpServletRequest request, HttpServletResponse response, String path, String[] arguments) throws ServletException, IOException {
		//对象名
		String className = null;
		String[] targetValue = null;
		String target = DEFAULT_METHOD;
		try {
			targetValue = this.getTargetValue(path);
			className = targetValue[0];
			target = targetValue[1];
			String moduleId=targetValue[2];
			ISecurityUser user = (ISecurityUser)request.getAttribute("SecurityUser");
			Security secure = new Security();
			if(!secure.isAdopt(moduleId)){
				return null;
			}
		} catch (Exception e) {
			LOG.error(e.getMessage());
			return null;
		}
		//反射
		Class<?> c = SysUtils.forName(className);
		if (c == null) {
			try {
				throw new Exception("没有找到对象:"+className);
			} catch (Exception e) {
				LOG.error(e.getMessage());
				return null;
			}
		}
		Method m = invokeMethod(c,target);
		//实例化对象
		Object o = SysUtils.newInstance(c);
		//执行对象中的执行目标
		String retrun_value = this.invoke(m, o, arguments, request, response);
		return retrun_value;
	}
	
	private String[] getTargetValue(String target) throws Exception{
		String[] value = InitAppPathMap.get(target);
		if(value == null){
			throw new Exception("没有找到路径:"+target);
		}
		return value;
	}
	
	/** - - 查找对象中的执行目标
	 * @param c -- 对象名
	 * @param methodName -- 方法名,如果没有就执行默认
	 * @return
	 */
	private Method invokeMethod(Class<?> c,String methodName){
		if(methodName == null){
			methodName = DEFAULT_METHOD;
		}
		Method[] m = c.getMethods();
		for (int i = 0; i < m.length; i++) {
			if(m[i].getName().equals(methodName)){
				return m[i];
			}
		}
			try {
				throw new Exception("在对象:"+c.getName()+" 中没有找到目标:"+methodName);
			} catch (Exception e) {
				LOG.error(e.getMessage());
			}
			return null;
	}
	
	/**执行对象中的执行目标
	 * @param m -- 执行目标
	 * @param o -- 执行对象
	 * @param arguments -- 参数
	 * @param request -- 参数
	 * @param response -- 参数
	 * @return -- 返回值
	 */
	private String invoke(Method m, Object o, String[] arguments, HttpServletRequest request, HttpServletResponse response){
		Class<?>[] types = m.getParameterTypes();
		int count=0;
		int num = 0;
		Object[] os = new Object[types.length];
		for (Class<?> cl : types) {
			if(cl.getName().equals("java.lang.String")&&count<arguments.length){
				os[num]=arguments[count];
				count++;
			}else if(cl.getName().equals("javax.servlet.http.HttpServletRequest")){
				os[num]=request;
			}else if(cl.getName().equals("javax.servlet.http.HttpServletResponse")){
				os[num]=response;
			}
			num++;
		}
		try {
			return (String)m.invoke(o, os);
		} catch (Exception e) {
			LOG.error("执行目标失败:"+o.getClass().getName()+" 方法:"+m.getName()+" 参数:"+os.toString()+":"+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
}
